﻿Ext.define('app.prm.document.Index', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    projectId: 0,
    initComponent: function () {
        var me = this;
        me.projectId = me.paramsId;

        me.items = [
            me.getTree(),
            {
                region: 'center',
                border: false,
                layout: 'border',
                items: [
                    me.getGrid(),
                    me.getVersionGrid()
                ]
            }];
        me.callParent();
    },
    getTree: function () {
        var me = this;
        if (!this.tree) {
            this.tree = new Ext.tree.Panel({
                region: 'west',
                width: 250,
                border: false,
                split: true,
                tbar: [{
                    text: '设置', icon: '/Content/images/edit.gif', handler: function () {
                        var win = Ext.create("app.prm.document.SetUploadType", { projectId: me.projectId });
                        win.on("savesuccess", function () {
                            me.getTree().store.load();
                            win.close();
                        });
                        win.show();
                    }
                }],
                title: '文档类型',
                useArrows: true,
                rootVisible: false,
                store: new Ext.data.TreeStore({
                    root: {
                        expanded: true
                    },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/document/GetProjectDocumentTree?projectId=' + me.projectId
                    }
                }),
                root: { id: '0', text: '' },
                listeners: {
                    afterrender: function (tree) { tree.expandAll(); },
                    select: function (tree, record, index, eOpts) {
                        if (record.data.leaf) {
                            me.getGrid().store.load({ params: { typeId: record.data.id, projectId: me.projectId } });
                        }
                    }
                }
            });
        }
        return this.tree;
    },
    getGrid: function () {
        var me = this;
        if (!this.grid) {
            this.grid = new Ext.grid.Panel({
                title: '文档列表',
                region: 'center',
                border: false,
                tbar: [
                    "->",
                    {
                        xtype: 'form',
                        id: 'fileform',
                        border: false,
                        items: [
                            {
                                xtype: 'filefield', width: 50, name: 'ProjectFile', buttonOnly: true, buttonText: '上传', listeners: {
                                    change: function (btn, value, eOpts) {
                                        var node = me.getTree().getSelection()[0];
                                        if (!node) {
                                            Ext.Msg.alert('系统提示', '请选择文档类型');
                                            return false;
                                        }
                                        Ext.getCmp("fileform").getForm().submit({
                                            url: '/api/services/app/document/UploadDocument',
                                            params: { ProjectId: me.projectId, TypeId: node.data.id },
                                            success: function (form, response) {
                                                me.getGrid().store.load({ params: { typeId: node.data.id, projectId: me.projectId } });
                                            }
                                        })
                                    }
                                }
                            }
                        ]
                    }
                ],
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/document/GetDocument', autoLoad: false }),
                columns: [
                    { header: "名称", dataIndex: 'name' },
                    { header: "大小(kb)", dataIndex: 'length' },
                    { header: "类型", dataIndex: 'extension' },
                    { header: "版本", dataIndex: 'versionNo' },
                    { header: "创建时间", dataIndex: 'creationTime' },
                    { header: "创建人", dataIndex: 'creationUser' },
                    {
                        xtype: 'actioncolumn',
                        width: 70,
                        header: '操作',
                        items: [
                        {
                            iconCls: 'edit',
                            tooltip: '上传文档',
                            handler: function (grid, rowIndex, colIndex) {
                                var record = grid.store.getAt(rowIndex);
                                var win = Ext.create("app.prm.document.Edit", { documentId: record.data.id });
                                win.on("savesuccess", function () {
                                    me.loadGrid();
                                    me.getVersionGrid().store.load({ params: { documentId: record.data.id } });
                                    win.close();
                                });
                                win.show();
                            }
                        },
                        {
                            iconCls: 'delete',
                            tooltip: '删除文档',
                            handler: function (grid, rowIndex, colIndex) {
                                var record = grid.store.getAt(rowIndex);
                                App.AjaxDelete('/api/services/app/document/RemoveDocument?documentId=' + record.data.id + "&projectId=" + me.projectId, function () {
                                    me.loadGrid();
                                });
                            }
                        }]
                    }
                ],
                listeners: {
                    select: function (grid, record, index, eOpts) {
                        me.getVersionGrid().store.load({ params: { documentId: record.data.id } });
                    }
                }
            });
        }
        return this.grid;
    },
    loadGrid: function () {
        var me = this;
        var node = me.getTree().getSelection()[0];
        me.getGrid().store.load({ params: { typeId: node.data.id, projectId: me.projectId } });
    },
    getVersionGrid: function () {
        if (!this.versionGrid) {
            this.versionGrid = new Ext.grid.Panel({
                title: '版本列表',
                height: 200,
                region: 'south',
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/document/GetDocumentVersion', autoLoad: false }),
                columns: [
                    { header: "名称", dataIndex: 'name' },
                    { header: "大小(kb)", dataIndex: 'length' },
                    { header: "类型", dataIndex: 'extension' },
                    { header: "版本", dataIndex: 'version' },
                    { header: "创建时间", dataIndex: 'creationTime' },
                    { header: "创建人", dataIndex: 'creationUser' }
                ]
            });
        }
        return this.versionGrid;
    }
});